package cn.qn.controller.news;

import cn.qn.base.PageVO;
import cn.qn.dto.ResponseDTO;
import cn.qn.dto.news.AlbumDTO;
import cn.qn.dto.news.AlbumQueryDTO;
import cn.qn.framwork.security.MemberAccountUtils;
import cn.qn.service.news.AlbumService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Jeet
 * @description
 * @date 2019/5/29 17:12
 */
@RestController
@RequestMapping("/news/album")
@Slf4j
public class AlbumController {

    @Reference(check = false)
    private AlbumService albumService;

    @RequestMapping("/insert")
    public ResponseDTO insert(@RequestBody AlbumDTO album){
        if(null == album.getName() || StringUtils.isBlank(album.getName())){
            return ResponseDTO.error("相册名称不能为空");
        }
        if(null == album.getName() || null == album.getCover()){
            return ResponseDTO.error("相册封面不能为空");
        }
        if(null == album.getName() || null == album.getBanner()){
            return ResponseDTO.error("相册Banner不能为空");
        }
        if(null == album.getName() || null == album.getPhotos() || album.getPhotos().isEmpty()){
            return ResponseDTO.error("相册图片不能为空");
        }

        album.setUpdateBy(MemberAccountUtils.getCurrentMember().getUserId());
        if(null == album.getId()){
            album.setCreateBy(MemberAccountUtils.getCurrentMember().getUserId());
            albumService.insert(album);
        }else{
            albumService.update(album);
        }

        return ResponseDTO.success(true,"保存成功");
    }

    @RequestMapping("/page")
    public ResponseDTO page(@RequestBody AlbumQueryDTO baseQuery){
        PageVO<AlbumDTO> albumDTOPageVO = albumService.page(baseQuery);
        return ResponseDTO.success(albumDTOPageVO);
    }

    @RequestMapping("/findById/{id}")
    public ResponseDTO findById(@PathVariable Integer id){
        AlbumDTO albumDTO = albumService.findById(id);
        return ResponseDTO.success(albumDTO);
    }
}
